                                                                                                                                                                                                                                                               
#include "StdAfx.h"
#include "Species.h"
#include "../TermEngine/Terms/ApplicationTerm.h"

Species::Species( Individual Adam ){
	Genus = static_cast<Type*>(_TERM(Adam->operator[ ](0)).getType( ).clone( ));
	Population.push_back( Adam );
	}

Species::~Species( ){
	delete Genus;
	}

Species::Species(const Species& Sp){
	Genus = static_cast< Type* >(Sp.Genus->clone( ));
	for(size_t i=0; i<size( ); i++)
		this->Population.push_back(Sp.Population[i]);
	}
		
Individual Species::insert( Individual SM){
	if( Species::areSameSpecie( SM, Population[0]) ){
		Population.push_back( SM );
		return SM;
		}
	return NULL;
	}


bool Species::areSameSpecie( Individual SM1, Individual SM2){
	return (_TERM(SM1->operator[ ](0)).getType( ) == _TERM(SM2->operator[ ](0)).getType( ));
	}

size_t Species::size( ) const{
	return Population.size( );
	}
bool Species::belongToSpecie(Individual SM) const{
	return Genus->operator ==(_TERM(SM->operator[ ](0)).getType( ));
	}
Individual Species::operator[ ](size_t index) const {
	if (index >= size( )) throw "index error at const SpecieMember* operator[ ](size_t index)";
	return Population[index];
	}		

const Type* Species::getGenus( void ) const{
	return Genus;
	}

size_t Species::linksTo( const Token* ToPtr){
	size_t Res = 0;
	for(size_t i=0; i<this->Population.size( ); i++)
		for(size_t j=0; j<Population[i]->size( ); j++)
			if(Population[i]->ptrAtIndex( j ) == ToPtr)
				Res++;

	return Res;

	}

 
